﻿//-----------------------------------------------------------------------------
//<filedescription file="ArrayBuilder.ArraySettingsDialog.cs" company="Microsoft">
//  <copyright>
//     Copyright © Microsoft Corporation.  All rights reserved.
//     ***  DESIGNATED SAMPLE CODE (per the included software license terms) ***
//      THIS CODE SAMPLE IS MADE AVAILABLE TO YOU WITHOUT WARRANTY OF ANY KIND
//      AND CONFERS NO RIGHTS ON YOU WHATSOEVER EXCEPT THE RIGHT TO REVIEW IT
//      SOLELY AS A REFERENCE.  THE ENTIRE RISK OF USE OR RESULTS FROM USE OF
//      THIS CODE SAMPLE REMAINS WITH YOU.
//  </copyright>
//  <purpose>
//     A dialog within the Array Builder UI framework that allows the user
//     to define settings specific to an array.
//
//  </purpose>
//  <notes>
//  </notes>
//</filedescription>
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Xml;
using System.Windows.Forms;
using Microsoft.ConfigurationManagement.OemDeployment;
using Microsoft.ConfigurationManagement.IntegrationDevelopmentKit;

namespace Microsoft.ConfigurationManagement.OemDeployment.ArrayBuilder
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ArraySettingsDialog : Form
    {
        #region Properties

        private const string ARRAY_NO_RAID = "None";
        private const string ARRAY_DYNAMIC = "Dynamic";
        
        //ArrayBuilderUtilities Utilities = new ArrayBuilderUtilities();

        private string arrayLevelSelected;
        /// <summary>
        /// 
        /// </summary>
        public string ArrayLevelSelected
        {
            get
            {
                return arrayLevelSelected;
            }
            set
            {
                arrayLevelSelected = value;
                string displayName = GetRaidDescription(arrayLevelSelected);
                if (comboBoxRaidLevel.Items.Contains(displayName))
                {
                    comboBoxRaidLevel.SelectedItem = displayName;
                }
            }
        }

        private XmlNode arrayLevelsXml;
        /// <summary>
        /// 
        /// </summary>
        public XmlNode ArrayLevelsXml
        {
            get { return arrayLevelsXml;}
            set { arrayLevelsXml = value; }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public XmlNode ArrayLevelSettings
        {
            get { return null; }
            set 
            {
                try
                {
                    comboBoxRaidLevel.Items.Clear();
                    comboBoxRaidLevel.Items.Add(Properties.Resources.ArrayBuilder_Text_Select);

                    if (value != null)
                    {
                        ArrayLevelsXml = value;
                        string raidLevel = null;
                        for (int i = 0; i < ArrayLevelsXml.ChildNodes.Count; i++)
                        {
                            raidLevel = IntegrationKitUtilities.GetXmlAttribute(ArrayLevelsXml.ChildNodes[i], "Name");
                            if (string.IsNullOrEmpty(raidLevel) == false)
                            {
                                string displayName = GetRaidDescription(raidLevel);

                                if (string.IsNullOrEmpty(displayName))
                                {
                                    throw new System.ArgumentException("displayName", Properties.Resources.ArrayBuilder_Error_InvalidParameter);
                                }
                                comboBoxRaidLevel.Items.Add(displayName);                              
                            }
                        }
                    }
                    else
                    {
                        //Load default settings  - RAID 0, 1, 5, None
                        
                        comboBoxRaidLevel.Items.Add(Properties.Resources.ArraySettings_RaidLevels_Raid0_Description);
                        comboBoxRaidLevel.Items.Add(Properties.Resources.ArraySettings_RaidLevels_Raid1_Description);
                        comboBoxRaidLevel.Items.Add(Properties.Resources.ArraySettings_RaidLevels_Raid5_Description);
                    }
                    comboBoxRaidLevel.SelectedItem = Properties.Resources.ArrayBuilder_Text_Select;
                }
                catch (Exception ex)
                {
                    Utilities.ShowExceptionMessage(ex, MethodBase.GetCurrentMethod());
                    
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public XmlNode OptionsValues
        {
            get
            {
                if (dataGridViewAdditionalOptions.Enabled == true)
                {
                    if (dataGridViewAdditionalOptions.RowCount > 0)
                    {
                        return Utilities.GetOptionsDataGridValues(dataGridViewAdditionalOptions, "ArrayOptions");
                    }
                }
                return null; 
            }
            set
            {
                dataGridViewAdditionalOptions = Utilities.FillOptionsDataGrid(dataGridViewAdditionalOptions, OemOptionsNode, value);
                //FillOptionsDataGrid(value); 
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool OptionsGridEnabled
        {
            get { return dataGridViewAdditionalOptions.Enabled; }
            set
            {
                dataGridViewAdditionalOptions.Enabled = value;
                if (value == false)
                {
                    dataGridViewAdditionalOptions.Visible = false;
                    groupBoxAdditionalOptions.Visible = false;
                    this.Height = 200;
                }
            }
        }

        private XmlNode oemOptionsNode;
        /// <summary>
        /// 
        /// </summary>
        public XmlNode OemOptionsNode
        {
            get { return oemOptionsNode; }
            set { oemOptionsNode = value; }
        }
        
        
        #endregion

        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        public ArraySettingsDialog()
        {
            InitializeComponent();
            SetupErrorProvider();
            this.TopMost = true;
        }


        #endregion

        private string GetRaidDescription(string raidLevel)
        {
            string displayName = null;
            switch (raidLevel)
            {
                case "0":
                    displayName = Properties.Resources.ArraySettings_RaidLevels_Raid0_DisplayName;
                    break;

                case "1":
                    displayName = Properties.Resources.ArraySettings_RaidLevels_Raid1_DisplayName;
                    break;

                case "5":
                    displayName = Properties.Resources.ArraySettings_RaidLevels_Raid5_DisplayName;
                    break;

                case "6":
                    displayName = Properties.Resources.ArraySettings_RaidLevels_Raid6_DisplayName;
                    break;

                case "10":
                    displayName = Properties.Resources.ArraySettings_RaidLevels_Raid10_DisplayName;
                    break;

                case "50":
                    displayName = Properties.Resources.ArraySettings_RaidLevels_Raid50_DisplayName;
                    break;

                case "60":
                    displayName = Properties.Resources.ArraySettings_RaidLevels_Raid60_DisplayName;
                    break;

                case ARRAY_DYNAMIC:
                    displayName = Properties.Resources.ArraySettings_RaidLevels_RaidDynamic_DisplayName;
                    break;

                case ARRAY_NO_RAID:
                    displayName = Properties.Resources.ArrayBuilder_Text_None;
                    break;

                default:
                    break;
            }
            return displayName;
        }


        #region Event handlers

        private void comboBoxRaidLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            ValidateArrayLevel();
            if (epArrayLevel.GetError(comboBoxRaidLevel) == "")
            {
                string selectedRaid = IntegrationKitUtilities.GetComboBoxControlValue(comboBoxRaidLevel);
                string description = null;

                if (string.IsNullOrEmpty(selectedRaid) == false)
                {
                    if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_Raid0_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_Raid0_Description;
                        ArrayLevelSelected = "0";
                    }
                    else if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_Raid1_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_Raid1_Description;
                        ArrayLevelSelected = "1";
                    }
                    else if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_Raid5_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_Raid5_Description;
                        ArrayLevelSelected = "5";
                    }
                    else if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_Raid6_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_Raid6_Description;
                        ArrayLevelSelected = "6";
                    }
                    else if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_Raid10_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_Raid10_Description;
                        ArrayLevelSelected = "10";
                    }
                    else if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_Raid50_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_Raid50_Description;
                        ArrayLevelSelected = "50";
                    }
                    else if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_Raid60_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_Raid60_Description;
                        ArrayLevelSelected = "60";
                    }
                    else if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_RaidDynamic_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_RaidDynamic_Description;
                        ArrayLevelSelected = ARRAY_DYNAMIC;
                    }
                    else if (string.Compare(selectedRaid, Properties.Resources.ArraySettings_RaidLevels_RaidNone_DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        description = Properties.Resources.ArraySettings_RaidLevels_RaidNone_Description;
                        ArrayLevelSelected = ARRAY_NO_RAID;
                    }
                    
                }
                if (string.IsNullOrEmpty(description) == false)
                {
                    richTextBoxRaidDescriptionText.Text = description;
                }
                buttonOK.Enabled = true;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (AllControlsAreValid())
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                Utilities.ShowNotificationMessage(Properties.Resources.ArraySettings_Error_InvalidControlsOnPage, Properties.Resources.Text_Error);
                this.Focus();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        #endregion

        #region Validation Methods

        private bool AllControlsAreValid()
        {
            try
            {
                bool valid = true;
                if (epArrayLevel.GetError(comboBoxRaidLevel) != "")
                {
                    valid = false;
                }
                if (epAdditionalOptions.GetError(dataGridViewAdditionalOptions) != "")
                {
                    valid = false;
                }
                return valid;
            }
            catch (Exception ex)
            {
                Utilities.ShowExceptionMessage(ex, MethodBase.GetCurrentMethod());
                return false;
            }
        }

        private void SetupErrorProvider()
        {
            epArrayLevel = new ErrorProvider();
            epArrayLevel.SetIconAlignment(comboBoxRaidLevel, ErrorIconAlignment.MiddleRight);
            epArrayLevel.SetIconPadding(comboBoxRaidLevel, 2);
            epArrayLevel.BlinkStyle = ErrorBlinkStyle.NeverBlink;
            epArrayLevel.SetError(comboBoxRaidLevel, "");

            epAdditionalOptions = new ErrorProvider();
            epAdditionalOptions.SetIconAlignment(dataGridViewAdditionalOptions, ErrorIconAlignment.MiddleRight);
            epAdditionalOptions.SetIconPadding(dataGridViewAdditionalOptions, 2);
            epAdditionalOptions.BlinkStyle = ErrorBlinkStyle.NeverBlink;
            epAdditionalOptions.SetError(dataGridViewAdditionalOptions, "");


        }

        private void ValidateArrayLevel()
        {

            try
            {
                string selected = IntegrationKitUtilities.GetComboBoxControlValue(comboBoxRaidLevel);
                if((string.IsNullOrEmpty(selected) == false) && 
                    (string.Compare(selected, Properties.Resources.ArrayBuilder_Text_Select, StringComparison.OrdinalIgnoreCase) == 0))
                {
                    // still on default selection
                    epArrayLevel.SetError(comboBoxRaidLevel, Properties.Resources.ArraySettings_Error_SelectAnArrayLevel);
                }
                else
                {
                    epArrayLevel.SetError(comboBoxRaidLevel, "");
                }
            }
            catch (Exception ex)
            {
                Utilities.ShowExceptionMessage(ex, MethodBase.GetCurrentMethod());
                //throw;
            }
        }

        private void ValidateDataGrid()
        {

        }

        #endregion

      
    }
}